package com.morven.workpiece.craft.service.impl;

import java.util.List;
import java.util.Map;

import com.morven.workpiece.common.utils.DateUtils;
import com.morven.workpiece.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.morven.workpiece.craft.mapper.CraftProceCategoryTypeMapper;
import com.morven.workpiece.craft.domain.CraftProceCategoryType;
import com.morven.workpiece.craft.service.ICraftProceCategoryTypeService;

/**
 * 工序类别Service业务层处理
 *
 * @author Morven
 * @date 2021-12-20
 */
@Service
public class CraftProceCategoryTypeServiceImpl implements ICraftProceCategoryTypeService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private CraftProceCategoryTypeMapper craftProceCategoryTypeMapper;

    /**
     * 查询工序类别
     *
     * @param id 工序类别主键
     * @return 工序类别
     */
    @Override
    public CraftProceCategoryType selectCraftProceCategoryTypeById(Long id) {
        return craftProceCategoryTypeMapper.selectCraftProceCategoryTypeById(id);
    }

    /**
     * 查询工序类别列表
     *
     * @param craftProceCategoryType 工序类别
     * @return 工序类别
     */
    @Override
    public List<CraftProceCategoryType> selectCraftProceCategoryTypeList(CraftProceCategoryType craftProceCategoryType) {
        return craftProceCategoryTypeMapper.selectCraftProceCategoryTypeList(craftProceCategoryType);
    }

    /**
     * 新增工序类别
     *
     * @param craftProceCategoryType 工序类别
     * @return 结果
     */
    @Override
    public int insertCraftProceCategoryType(CraftProceCategoryType craftProceCategoryType) {
        craftProceCategoryType.setCreateTime(DateUtils.getNowDate());
        return craftProceCategoryTypeMapper.insertCraftProceCategoryType(craftProceCategoryType);
    }

    /**
     * 修改工序类别
     *
     * @param craftProceCategoryType 工序类别
     * @return 结果
     */
    @Override
    public int updateCraftProceCategoryType(CraftProceCategoryType craftProceCategoryType) {
        return craftProceCategoryTypeMapper.updateCraftProceCategoryType(craftProceCategoryType);
    }

    /**
     * 批量删除工序类别
     *
     * @param ids 需要删除的工序类别主键
     * @return 结果
     */
    @Override
    public int deleteCraftProceCategoryTypeByIds(Long[] ids) {
        return craftProceCategoryTypeMapper.deleteCraftProceCategoryTypeByIds(ids);
    }

    /**
     * 删除工序类别信息
     *
     * @param id 工序类别主键
     * @return 结果
     */
    @Override
    public int deleteCraftProceCategoryTypeById(Long id) {
        return craftProceCategoryTypeMapper.deleteCraftProceCategoryTypeById(id);
    }

    @Override
    public String checkCategory(Long[] ids) {
        StringBuilder strIds = new StringBuilder();
        for (Long id : ids) {
            strIds.append(id).append(",");
        }
        strIds.append("-1");
        StringBuilder result = new StringBuilder();

        List<Map<String, Object>> parentCategoryList = jdbcTemplate.queryForList("select proce_category_type from  craft_proce_category_type where id in (" + strIds + ")");
        parentCategoryList.forEach(item -> {
            result.append(item.get("proce_category_type").toString()).append(",");
        });
        if (!StringUtils.isEmpty(result.toString())) {
            return result.delete(result.length() - 1, result.length()).toString();
        }
        List<Map<String, Object>> coefficList = jdbcTemplate.queryForList("select proce_category_type_name from craft_proce_wage_coeffic where proce_category_type_id in (" + strIds + ")");
        coefficList.forEach(item -> {
            result.append(item.get("proce_category_type_name").toString()).append(",");
        });
        if (!StringUtils.isEmpty(result.toString())) {
            return result.delete(result.length() - 1, result.length()).toString();
        }
        return null;
    }
}
